import '../config/api_constants.dart';

abstract class ApiException implements Exception {
  final int code;
  final String message;
  final dynamic data;

  const ApiException({
    required this.code,
    required this.message,
    this.data,
  });

  @override
  String toString() => '[$code] $message';
}

// 具体异常类型
class NetworkException extends ApiException {
  const NetworkException()
      : super(
          code: ApiConstants.networkErrorCode,
          message: '网络连接异常',
        );
}

class UnauthorizedException extends ApiException {
  const UnauthorizedException()
      : super(
          code: ApiConstants.invalidTokenCode,
          message: '认证已过期',
        );
}

class BusinessException extends ApiException {
  const BusinessException(
      {required super.code, required super.message, super.data});
}

class ServerException extends ApiException {
  const ServerException()
      : super(
          code: ApiConstants.serviceErrorCode,
          message: '服务器异常',
        );
}

/// 未知错误
class UnknowResponseException extends ApiException {
  const UnknowResponseException(dynamic data)
      : super(code: ApiConstants.unknownErrorCode, message: '未知错误', data: data);
}
